<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Poster; 
use app\model\Worker; 

class WInit extends Base
{
    public function W_init()
    {
global $_G, $_P;
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$system=$_G['system'];
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}

$company = intval($_P['company']);
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
if (empty($theme['softsn'])) {
	return_msg('软件未激活,请联系' . $_G['companyname'] . '工作人员');
}

$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}

$do = 'w_init';
//load_func('tpl');
if (empty($config['workertuan'])) {
	$config['workertuan']=5;
}
$wx = $_G['platform']['jssdkconfig']; 

$myworker = Worker::getThemeWorkerByOpenidzhuce($_G['openid'],$themeid,1);
$shoplist = db_select ( "SELECT * FROM " . tablename (t_shangjia) .
		" WHERE  state = 1 order by displayorder" );

$url= $_G['siteroot'].$this->createMobileUrl('w_init',array("themeid"=>$themeid,"soft"=>$theme['softkey'],"server_openid"=>$_P['server_openid']));

$uploadurl= $_G['siteroot'].$this->createMobileUrl('w_init',array("op"=>'upload',"themeid"=>$themeid,"soft"=>$theme['softkey'],"server_openid"=>$_P['server_openid']));
// echo $uploadurl;
if ($op=='upload') {
	$r=downloadFromWxServer($_P['bid']);
	$r[0]['remotepath'] = str_replace('http://', 'https://', $r[0]['remotepath']);
	return_msg($r[0],'','ajax'); 
	exit;
 
}

if ($op == 'display' && $system['tixing']==1 && $theme['status']==1) {
	if (empty($_P['server_openid'])) {
		return_msg('系统开启员工成交提醒，参数错误，请重新扫码！',$url,'error'); 
	}
	$data = array(
		'server'  => DOMAIN0,
		'softname'  => MODULE_NAME,
        'uniacid' 	=> $_G['mpid'],
		'themeid' 	=> $themeid,
		'softkey' 	=> $theme['softkey'],
		'server_openid' 	=> $_P['server_openid'],
		'soft_openid' 	=> $fans['openid'],
		'username'=> $_P['username'],
		'mobile'  => $_P['mobile'],
		'zhiwu'  => $_P['zhiwu'],		
	);
	$qr = Poster::ServerQR($system,'save',$data);
	if ($qr['error']!=0) {
		return_msg($qr['msg'],$url,'error'); 
	}
}

if ($op == 'next') {
	$pass = $_P['pass'];
	if(empty($pass)){
		return_msg('请输入密码',$url,'error'); 
	}
	if ($system['ruanjian']==3){
		if(empty($company)){
			return_msg('请选择所属企业',$url,'error'); 
		}
		$shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$company}'");
		$shopname=$shop['name'];
		$shoppass=$shop['shoppass'];
		$workerpass=$shop['workerpass'];
	}else{
		$workerpass=$config['workerpass'];
	}
	if ($workerpass == $pass){
		$level = 'worker';
		$op='last';
		$leveln = '3';
	}elseif ($shoppass == $pass){
		$level = 'shop';
		$leveln = '2';
	}elseif ($system['adminpass'] == $pass){
		$level = 'admin';
		$leveln = '1';
	}else{
		return_msg('密码错误',$url,'error'); 
	}

	if ($system['ruanjian']==3){
		if ($level == 'worker' || $level == 'shop'){
			if ($myworker['company']!=$company && $myworker['company']!=0){
				// $url= $_G['siteroot'].createMobileUrl('w_init',array("themeid"=>$themeid));
				return_msg('您不可变更企业,如需变更请联系管理员,进行人事变动',$url,'error'); 
			}
		}
	}

	$data = array (
		'uniacid' => $_G['mpid'],
		'uid'     => $_G['member']['uid'],
		'openid'  => $_G['openid'],
		'status'  => 1,
		'level'  => $leveln,
		'type'    => 'theme',
	);


	$alltheme = Theme::getAllTheme();
	if (!empty($alltheme)) {
		foreach ($alltheme as $key => $item) {
			$data['themeid'] = $item['id'];
			$workersave = Worker::getThemeWorkerByOpenidzhuce($_G['openid'],$item['id'],1);
			if (empty($workersave)){
				$data ['addtime'] = TIMESTAMP;
				db_insert ( t_worker, $data );
			}else{
				db_update (t_worker, $data, array ('openid' => $_G['openid'],'themeid' => $item['id']));
			}
		}
	}
	if ($level == 'admin' || $level == 'shop'){
		$teamlist = db_select ( "SELECT * FROM " . tablename (t_team) .
			" WHERE  company = '{$company}' order by groupid" );
		$tuanshu = 5;
		foreach ($teamlist as $index =>$row){
	        if ($row['groupid']==1){
	        	$tuan1 = $row['name'];
	        }elseif ($row['groupid']==2){
	        	$tuan2 = $row['name'];
	        }elseif ($row['groupid']==3){
	        	$tuan3 = $row['name'];
	        }elseif ($row['groupid']==4){
	        	$tuan4 = $row['name'];
	        }elseif ($row['groupid']==5){
	        	$tuan5 = $row['name'];
	        }elseif ($row['groupid']==6){
	        	$tuan6 = $row['name'];
	        	if ($tuanshu < 6) {
	        		$tuanshu = 6;
	        	}
	        }elseif ($row['groupid']==7){
	        	$tuan7 = $row['name'];
	        	if ($tuanshu < 7) {
		        	$tuanshu = 7;
		        }
	        }elseif ($row['groupid']==8){
	        	$tuan8 = $row['name'];
	        	if ($tuanshu < 8) {
		        	$tuanshu = 8;
		        }
	        }elseif ($row['groupid']==9){
	        	$tuan9 = $row['name'];
	        	if ($tuanshu < 9) {
		        	$tuanshu = 9;
		        }
	        }elseif ($row['groupid']==10){
	        	$tuan10 = $row['name'];
	        	if ($tuanshu < 10) {
		        	$tuanshu = 10;
		        }
	        }elseif ($row['groupid']==11){
	        	$tuan11 = $row['name'];
	        	if ($tuanshu < 11) {
		        	$tuanshu = 11;
		        }
	        }elseif ($row['groupid']==12){
	        	$tuan12 = $row['name'];
	        	if ($tuanshu < 12) {
		        	$tuanshu = 12;
		        }
	        }elseif ($row['groupid']==13){
	        	$tuan13 = $row['name'];
	        	if ($tuanshu < 13) {
		        	$tuanshu = 13;
		        }
	        }elseif ($row['groupid']==14){
	        	$tuan14 = $row['name'];
	        	if ($tuanshu < 14) {
		        	$tuanshu = 14;
		        }
	        }elseif ($row['groupid']==15){
	        	$tuan15 = $row['name'];
	        	if ($tuanshu < 15) {
		        	$tuanshu = 15;
		        }
	        }elseif ($row['groupid']==16){
	        	$tuan16 = $row['name'];
	        	if ($tuanshu < 16) {
		        	$tuanshu = 16;
		        }
	        }elseif ($row['groupid']==17){
	        	$tuan17 = $row['name'];
	        	if ($tuanshu < 17) {
		        	$tuanshu = 17;
		        }
	        }elseif ($row['groupid']==18){
	        	$tuan18 = $row['name'];
	        	if ($tuanshu < 18) {
		        	$tuanshu = 18;
		        }
	        }elseif ($row['groupid']==19){
	        	$tuan19 = $row['name'];
	        	if ($tuanshu < 19) {
		        	$tuanshu = 19;
		        }
	        }elseif ($row['groupid']==20){
	        	$tuan20 = $row['name'];
	        	if ($tuanshu < 20) {
		        	$tuanshu = 20;
		        }
	        }elseif ($row['groupid']==21){
	        	$tuan21 = $row['name'];
	        	if ($tuanshu < 21) {
		        	$tuanshu = 21;
		        }
	        }elseif ($row['groupid']==22){
	        	$tuan22 = $row['name'];
	        	if ($tuanshu < 22) {
		        	$tuanshu = 22;
		        }
	        }elseif ($row['groupid']==23){
	        	$tuan23 = $row['name'];
	        	if ($tuanshu < 23) {
		        	$tuanshu = 23;
		        }
	        }elseif ($row['groupid']==24){
	        	$tuan24 = $row['name'];
	        	if ($tuanshu < 24) {
		        	$tuanshu = 24;
		        }
	        }elseif ($row['groupid']==25){
	        	$tuan25 = $row['name'];
	        	if ($tuanshu < 25) {
		        	$tuanshu = 25;
		        }
	        }elseif ($row['groupid']==26){
	        	$tuan26 = $row['name'];
	        	if ($tuanshu < 26) {
		        	$tuanshu = 26;
		        }
	        }elseif ($row['groupid']==27){
	        	$tuan27 = $row['name'];
	        	if ($tuanshu < 27) {
		        	$tuanshu = 27;
		        }
	        }elseif ($row['groupid']==28){
	        	$tuan28 = $row['name'];
	        	if ($tuanshu < 28) {
		        	$tuanshu = 28;
		        }
	        }elseif ($row['groupid']==29){
	        	$tuan29 = $row['name'];
	        	if ($tuanshu < 29) {
		        	$tuanshu = 29;
		        }
	        }elseif ($row['groupid']==30){
	        	$tuan30 = $row['name'];
	        	if ($tuanshu < 30) {
		        	$tuanshu = 30;
		        }
	        }
		}
	}


}
if ($op == 'last') {
	if ($_P['jiami']!='jiami') {
		return_msg("访问出错，请重新扫描二维码",$url,'error'); 
	}
	$level = $_P['level'];
	if ($system['ruanjian']==3){
		$shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$company}'");
		$shopname=$shop['name'];
	}
	if ($level == 'admin' || $level == 'shop'){	
		$tuan1 = $_P['tuan1'];
		$tuan2 = $_P['tuan2'];
		$tuan3 = $_P['tuan3'];
		$tuan4 = $_P['tuan4'];
		$tuan5 = $_P['tuan5'];
		$tuan6 = $_P['tuan6'];
		$tuan7 = $_P['tuan7'];
		$tuan8 = $_P['tuan8'];
		$tuan9 = $_P['tuan9'];
		$tuan10 = $_P['tuan10'];
		$tuan11 = $_P['tuan11'];
		$tuan12 = $_P['tuan12'];
		$tuan13 = $_P['tuan13'];
		$tuan14 = $_P['tuan14'];
		$tuan15 = $_P['tuan15'];
		$tuan16 = $_P['tuan16'];
		$tuan17 = $_P['tuan17'];
		$tuan18 = $_P['tuan18'];
		$tuan19 = $_P['tuan19'];
		$tuan20 = $_P['tuan20'];
		$tuan21 = $_P['tuan21'];
		$tuan22 = $_P['tuan22'];
		$tuan23 = $_P['tuan23'];
		$tuan24 = $_P['tuan24'];
		$tuan25 = $_P['tuan25'];
		$tuan26 = $_P['tuan26'];
		$tuan27 = $_P['tuan27'];
		$tuan28 = $_P['tuan28'];
		$tuan29 = $_P['tuan29'];
		$tuan30 = $_P['tuan30'];
		Worker::addTeam($company,$themeid,1,$tuan1);
		Worker::addTeam($company,$themeid,2,$tuan2);
		Worker::addTeam($company,$themeid,3,$tuan3);
		Worker::addTeam($company,$themeid,4,$tuan4);
		Worker::addTeam($company,$themeid,5,$tuan5);
		Worker::addTeam($company,$themeid,6,$tuan6);
		Worker::addTeam($company,$themeid,7,$tuan7);
		Worker::addTeam($company,$themeid,8,$tuan8);
		Worker::addTeam($company,$themeid,9,$tuan9);
		Worker::addTeam($company,$themeid,10,$tuan10);
		Worker::addTeam($company,$themeid,11,$tuan11);
		Worker::addTeam($company,$themeid,12,$tuan12);
		Worker::addTeam($company,$themeid,13,$tuan13);
		Worker::addTeam($company,$themeid,14,$tuan14);
		Worker::addTeam($company,$themeid,15,$tuan15);
		Worker::addTeam($company,$themeid,16,$tuan16);
		Worker::addTeam($company,$themeid,17,$tuan17);
		Worker::addTeam($company,$themeid,18,$tuan18);
		Worker::addTeam($company,$themeid,19,$tuan19);
		Worker::addTeam($company,$themeid,20,$tuan20);
		Worker::addTeam($company,$themeid,21,$tuan21);
		Worker::addTeam($company,$themeid,22,$tuan22);
		Worker::addTeam($company,$themeid,23,$tuan23);
		Worker::addTeam($company,$themeid,24,$tuan24);
		Worker::addTeam($company,$themeid,25,$tuan25);
		Worker::addTeam($company,$themeid,26,$tuan26);
		Worker::addTeam($company,$themeid,27,$tuan27);
		Worker::addTeam($company,$themeid,28,$tuan28);
		Worker::addTeam($company,$themeid,29,$tuan29);
		Worker::addTeam($company,$themeid,30,$tuan30);
	}
	$teamlist = db_select ( "SELECT * FROM " . tablename (t_team) .
		" WHERE  company = '{$company}' order by groupid" );
}

if ($op == 'save') {
	$team = $_P['team'];
	$teamlist = db_select ( "SELECT * FROM " . tablename (t_team) .
		" WHERE  company = '{$company}' order by groupid" );
	if (!empty($teamlist) && empty($team)){
		return_msg("请选择所属团队",$url,'error'); 
	}
	if (empty($_P['username'])){
		return_msg("请输入昵称!",$url,'error'); 
	}
	if (empty($_P['mobile'])){
		return_msg("请输入电话!",$url,'error'); 
	}
	if ($system['tixing']==1 && $theme['status']==1) {
		$data = array(
			'server'  => DOMAIN0,
			'softname'  => MODULE_NAME,
	        'uniacid' 	=> $_G['mpid'],
			'themeid' 	=> $themeid,
			'softkey' 	=> $theme['softkey'],
			'server_openid' 	=> $_P['server_openid'],
			'soft_openid' 	=> $fans['openid'],
			'username'=> $_P['username'],
			'mobile'  => $_P['mobile'],
			'zhiwu'  => $leveln,		
		);
		$qr = Poster::ServerQR($system,'save',$data);
		if ($qr['error']!=0) {
			return_msg($qr['msg'],$url,'error'); 
		}else{
			$qruserid = $qr['id'];
		}
	}else{
		$qruserid = 0;
	}
    $qrimg = Poster::base64_image_content($_P['qrcode']);
	$data = array (
		'uniacid' => $_G['mpid'],
		'nickname'=> $fans['nickname'],
		'avatar'  => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
		'uid'     => $_G['member']['uid'],
		'openid'  => $_G['openid'],
		'company' => $company,
		'team' => $team,
		'username'=> $_P['username'],
		'mobile'  => $_P['mobile'],
		'qrcode'	  => $qrimg,
		'status'  => 1,
		'qruserid'  => $qruserid,
		'type'    => 'theme',
	);

	$alltheme = Theme::getAllTheme();
	if (!empty($alltheme)) {
		foreach ($alltheme as $key => $item) {
			$data ['themeid'] = $item['id'];
			$workersave = Worker::getThemeWorkerByOpenidzhuce($_G['openid'],$item['id'],1);
			if (empty($workersave)){
				$data ['addtime'] = TIMESTAMP;
				db_insert ( t_worker, $data );
			}else{
				db_update (t_worker, $data, array ('openid' => $_G['openid'],'themeid' => $item['id']));
			}
            //判断是否有设置首图海报，有则直接生成
            $themeid = $item['id'];
            $poster =  Poster::getByThemeIdnew($themeid,1);
            if(!empty($poster['id'])){
                $member = array(
                    "openid"   =>$fans['openid'],
                    'nickname' =>$fans['nickname'],
                    'avatar'   =>$fans['avatar'],
                    // 'shareurl' => $shareurl,
                    'themeid' =>$themeid,
                    'addtime' => TIMESTAMP,
                );
                $poster['bg']=str_replace("/99.jpg","/haibao.jpg",$poster['bg']);

                $img = Poster::createGoodsPoster($poster, $member, $qrimg);
                if ($img['error'] == 1) {
                            return_msg($img['msg']);
                        }
                        $hbimg = str_replace("http://", "https://", $img['img']) ;
                db_update(t_worker, array('poster' =>$hbimg), array( 'themeid' => $themeid, 'openid' => $_G['openid']));
            }
		}
	}
	db_query("UPDATE ".tablename(t_relation)." SET parentid = ".$_G['member']['uid'].",popenid='".$_G['openid']."',workerid = ".$_G['member']['uid']." WHERE openid ='".$_G['openid']."'");

// 	$url=$_G['siteroot'].createMobileUrl('w_init',array("themeid"=>$themeid,"op"=>'finish'));
// 	return_msg('员工信息保存成功',$url,'success');    
// }
// if ($op == 'finish') {
	$shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$myworker['company']}'");
	$shopname=$shop['name'];

	$team=db_find("SELECT * FROM ".tablename(t_team)." WHERE id= '{$myworker['team']}'");
	$teamname=$team['name'];

	if ($myworker['level'] == '1'){	
		$shenfen="超级管理员";
	}elseif ($myworker['level'] == '2'){	
		$shenfen="商家管理员";
	}elseif ($myworker['level'] == '3'){	
		$shenfen="员工";
	}

	$shenfen='【'.$shenfen.'】';
	$sharedata = array(
	    'themeid'=>$themeid,
	    'fopenid'=>$_G['openid'],
	    'fromuid'=>$_G['member']['uid'],
	    'workerid'=>$myworker['uid'],
	    'flag'=>1,
	);

	db_update(t_fans, array('poster' =>''), array('uid'=>$_G['member']['uid']));

    if ($system['gongzhong'] =='1'){
    	$domaintxt = $system['domainhead1'];
    }else{
    	$domaintxt = DOMAIN0;
    }
	$workerurl = $_G['url_scheme'].$domaintxt."/".createMobileUrl('index',$sharedata);

	$workerurl = Poster::GetWorkerUrl($system,$workerurl,MODULE_NAME ."员工:".$myworker['username']);
	$return_clicent = get_defined_vars();
return view('/yxworker/w_init_success', $return_clicent);
	exit;
}
if($_G['companyname'] =='飞驰软件'){
	$return_clicent = get_defined_vars();
return view('/yxworker/w_init' , $return_clicent);
}else{
	$return_clicent = get_defined_vars();
return view('/yxworker/w_init' , $return_clicent);
}
}
}